package com.renbang.utils.drawandindex;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;

import com.renbang.dao.art.ArtIdDao;
import com.renbang.model.art.ArtId;
import com.renbang.model.art.ArtJian;
import com.renbang.service.art.ArtIdService;
import com.renbang.service.art.ArtJianService;

public class JsoupForJianshu {
	@Autowired
	ArtIdService artIdService;
	@Autowired
	ArtJianService artJianService;
//	Logger log = Logger.getLogger(JsoupForJianshu.class);
	public List<ArtJian> aticles = new ArrayList<ArtJian>();
	public List<String> visited = new ArrayList<String>();
	static List<ArtJian> allActicleliList;

	// 解析简书文章列表和内容的方法
	public boolean crawlerForJianShu() {
		// 首页文章列表列表
		try {
			allActicleliList = getActicleList("http://www.jianshu.com");
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		for (ArtJian ArtJian : allActicleliList) {// 主页文章li列表
			// 进入文章链接拿到文章完整内容放到文章列表。
			ArtJian article = null;
			try {
				article = getActicle(ArtJian);
			} catch (Exception e) {
				e.printStackTrace();
				return false;
			}
			if (article != null) {
				aticles.add(article);// 把主页里当前文章加入列表
				System.out.println(article.getTitle());
			}
			// 拿到当前文章作者的文章列表
			List<ArtJian> aAuthorActicleliList;
			try {
				aAuthorActicleliList = getActicleList(ArtJian
						.getAuthorLink());
			} catch (Exception e) {
				e.printStackTrace();
				return false;
			}
			for (ArtJian ArtJian2 : aAuthorActicleliList) {
				// 拿到列表下当前文章
				ArtJian article1 = null;
				try {
					article1 = getActicle(ArtJian2);
				} catch (Exception e) {
					e.printStackTrace();
					return false;
				}
				if (article1 != null) {
					aticles.add(article1);
					System.out.println(article1.getTitle());
				}

			}
			// 作者文章列表列表 //进入这个作者主页，拿到他的文章列表，再访问这些文章//判断文章链接是否反问过
		}
		System.out.println("准备批量插入记录");
		artJianService.batchAdd(aticles);
		System.out.println("准备创建索引");
		try {// 创建索引
			IndexManager.createIndex(aticles);
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}

	public static List<ArtJian> getActicleList(String urlStr)
			throws Exception {
		Thread.sleep(3000);
		Document document = Jsoup
				.connect(urlStr)
				.userAgent(
						"Mozilla/5.0 (Windows NT 6.3; WOW64; rv:52.0) Gecko/20100101 Firefox/52.0")
				.get();
		Elements noteList = document.select("ul.note-list");
		Elements li = noteList.select("li");
		List<ArtJian> mBeans = new ArrayList<ArtJian>();
		for (Element element : li) {
			ArtJian bean = new ArtJian();
			bean.setAuthorName(element.select("div.name").text()); // 作者姓名
			bean.setAuthorLink(element.select("a.blue-link").attr("abs:href")); // 作者首页链接
			bean.setTime(element.select("span.time").attr("data-shared-at")); // 发表时间
			bean.setTitle(element.select("a.title").text()); // 标题
			bean.setTitleLink(element.select("a.title").attr("abs:href")); // 标题链接
			bean.setContent(element.select("p.abstract").text()); // 内容摘要
			mBeans.add(bean);
		}
		// for (ArtJian ArtJian : mBeans) {
		// System.out.println(ArtJian);
		// }
		return mBeans;

	}

	// 文章内容
	public ArtJian getActicle(ArtJian article) throws Exception {
		Thread.sleep(3000);
		// 判断有没有访问过
		if (visited.contains(article.getTitleLink())) {
			return null;
		}
		visited.add(article.getTitleLink());
		Document doucument = Jsoup
				.connect(article.getTitleLink())
				.userAgent(
						"Mozilla/5.0 (Windows NT 6.3; WOW64; rv:52.0) Gecko/20100101 Firefox/52.0")
				.get();
		Elements articleContent = doucument.select("div.show-content");
		// Elements elements = doucument.select("div.note");
		// elements.select("div.meta-bottom").remove();//拿div和样式
		// elements.select("a.notebook").removeAttr("href");
		// Element div = elements.first();
		ArtId artId = new ArtId();
		artId.setTitle(article.getTitle());
		// 先插入一条记录
		artIdService.add(artId);
		// 得到刚刚插入的id
		article.setId(artIdService.findLastId());
		article.setContent(articleContent.text());
		return article;
	}
}